/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sigmatrix.h5.dao.DisTerminalShopDao;
import com.sigmatrix.h5.dao.DisTerminalShopDetailDao;
import com.sigmatrix.h5.dto.DisTerminalShopData;
import com.sigmatrix.h5.entity.DisTerminalShop;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.h5.utils.Utils;
import com.sigmatrix.sc.jdbc.common.dao.DaoImpl;

/**
 *@ClassName: DisTerminalShopDaoImpl
 *@Description: 
 *@author MH
 *@date 2017年8月14日 上午10:40:10
 */
@Repository
public class DisTerminalShopDaoImpl extends DaoImpl<DisTerminalShop,Integer> implements DisTerminalShopDao{
	
	@Autowired
	private DisTerminalShopDetailDao disTerminalShopDetailDao;
	
	@Override
	public Integer updateStatusByCode(String code, Short status) {
		final StringBuffer sql = new StringBuffer();
		sql.append("update ").append(tableName()).append(" set ");
		sql.append(" _status = ").append(status);
		sql.append(",_update_time = ?");
		sql.append(" where _code = ?");
		logger.info("sql="+sql.toString());
		logger.info("param=["+code+","+status+"]");
		return jdbcTemplate.update(sql.toString(),new Date(), code);
	}

	@Override
	public List<DisTerminalShop> findByCodeConstantDelete(String code) {
		final StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(tableName());
		sql.append(" where _code = ?");
		logger.info(sql.toString());
		logger.info("param=["+code+"]");
		return jdbcTemplate.query(sql.toString(), new DefaultRowMapper(), code);
	}

	@Override
	public DisTerminalShop findByName(String name) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(tableName());
		sql.append(" where _name = N'").append(name).append("' and _status <> ").append(Constants.STATUS_DELETE);
		logger.info(sql.toString());
		List<DisTerminalShop> list = jdbcTemplate.query(sql.toString(), new DefaultRowMapper());
		return CommonUtils.isEmpty(list)?null:list.get(0);
	}

	@Override
	public List<DisTerminalShop> findByOpenId(String openId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select  b.*	from ");
		sql.append(disTerminalShopDetailDao.tableName()).append(" a,");
		sql.append(tableName()).append(" b ");
		sql.append("where a._terminal_shop_id = b._id and a._status = ");
		sql.append(Constants.STATUS_ENABLE);
		sql.append(" and a._open_id = ?");
		sql.append(" order by a._create_time desc ");
		logger.info("sql="+sql.toString());
		logger.info(String.format("param={%s}",openId));
		return jdbcTemplate.query(sql.toString(), new DefaultRowMapper(), openId);
	}
	@Override
	public DisTerminalShop findByOpenIdAndDealerId(String openId,Integer dealerId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select  b.*	from ");
		sql.append(disTerminalShopDetailDao.tableName()).append(" a,");
		sql.append(tableName()).append(" b ");
		sql.append("where a._terminal_shop_id = b._id and a._status = ");
		sql.append(Constants.STATUS_ENABLE);
		sql.append(" and a._open_id = ?");
		sql.append(" and a._dealer_id = ?");
		sql.append(" order by a._create_time desc ");
		logger.info("sql="+sql.toString());
		logger.info(String.format("param={%s,%s}",openId,dealerId));
		List<DisTerminalShop> list = jdbcTemplate.query(sql.toString(), new DefaultRowMapper(), openId,dealerId);
		return CommonUtils.isEmpty(list)?null:list.get(0);
	}
	@Override
	public Integer submitAgin(String code) {
		StringBuffer sql = new StringBuffer();
		sql.append("update ").append(tableName());
		sql.append(" set _status = ").append(Constants.TERMINAL_SHOP_STATUS_NO_AUDIT);
		sql.append(" where _status = ").append(Constants.STATUS_DELETE);
		sql.append(" and _code = ?");
		logger.info("sql="+sql.toString());
		logger.info(String.format("param={%s}",code));
		return jdbcTemplate.update(sql.toString(), code);
	}
	
	/**
	 * 分销app合并分支开始-------------------------------------
	 */
	
	@Autowired
	private transient JdbcTemplate jdbcTemplate;

	public static final String DISTRIBUTION_TERMINAL_SHOP = "_distribution_terminal_shop";
	
	public static final String DISTRIBUTION_TERMINAL_SHOP_DETAIL = "_distribution_terminal_shop_detail";

	@Override
	public List<DisTerminalShopData> findByDealerId(Integer dealerId,String enterpriseCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select  b.*	from ");
		sql.append(getTname(enterpriseCode, DISTRIBUTION_TERMINAL_SHOP_DETAIL)).append(" a,");
		sql.append(getTname(enterpriseCode, DISTRIBUTION_TERMINAL_SHOP)).append(" b ");
		sql.append("where a._terminal_shop_id = b._id and a._status = ");
		sql.append(Constants.STATUS_ENABLE);
		List<Object> paramList = new ArrayList<Object>();
		sql.append(" and a._dealer_id = ?");
		paramList.add(dealerId);
		logger.info("sql="+sql.toString());
		return jdbcTemplate.query(sql.toString(), paramList.toArray(), new DisTerminalShopDataRowMapper());
	}

	@Override
	public List<DisTerminalShopData> findByType(short type,String enterpriseCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select  a.*	from ");
		sql.append(getTname(enterpriseCode, DISTRIBUTION_TERMINAL_SHOP)).append(" a ");
		sql.append("where a._status = ");
		sql.append(Constants.STATUS_ENABLE);
		List<Object> paramList = new ArrayList<Object>();
		sql.append(" and a._type = ?");
		paramList.add(type);
		logger.info("sql="+sql.toString());
		return jdbcTemplate.query(sql.toString(), paramList.toArray(), new DisTerminalShopDataRowMapper());
	}
	
	@Override
	public DisTerminalShopData findById(Integer id, String enterpriseCode) {
		StringBuffer sql = new StringBuffer();
		sql.append("select  a.*	from ");
		sql.append(getTname(enterpriseCode, DISTRIBUTION_TERMINAL_SHOP)).append(" a ");
		sql.append("where a._status = ");
		sql.append(Constants.STATUS_ENABLE);
		List<Object> paramList = new ArrayList<Object>();
		sql.append(" and a._id = ?");
		paramList.add(id);
		logger.info("sql="+sql.toString());
		List<DisTerminalShopData> list = jdbcTemplate.query(sql.toString(), paramList.toArray(), new DisTerminalShopDataRowMapper());
		if (Utils.isNotEmpty(list)) {
			return list.get(0);
		}else{
			return null;
		}
	}
	
	public class DisTerminalShopDataRowMapper implements RowMapper<DisTerminalShopData> {
		public DisTerminalShopData mapRow(final ResultSet resultSet, final int rowNum) throws SQLException {
			final DisTerminalShopData disTerminalShopData = new DisTerminalShopData();
			disTerminalShopData.setId(resultSet.getInt("_id"));
			disTerminalShopData.setName(resultSet.getString("_name"));
			disTerminalShopData.setCode(resultSet.getString("_code"));
			return disTerminalShopData;
		}
	}
	
	
	
	/**是否为分表策略,为true,表名加上传递的企业code*/
	public static final boolean ISSUBMETER = true;
	
    protected String getTname(final String eseCode, final String tName) {
		
		final StringBuilder sb = new StringBuilder();
		if(ISSUBMETER) { // 分表
			if(Utils.isNotEmpty(eseCode)){//企业code不为空
				sb.append("_").append(eseCode);
			}
		}
		sb.append(tName);
		return sb.toString();
	}

    
	/**
	 * 分销app合并分支结束-------------------------------------
	 */
}
